package ClientCommands;

import java.io.IOException;
import java.util.HashMap;

import org.json.simple.JSONObject;

import ServiceLayer.ServiceFacade;

import Annotations.IsClientCommand;
import Commands.ClientCommandBase;
import Commands.ExecuteMethod;
import Helpers.ApplicationLogger;

@IsClientCommand
public class AddNewUserClientCommand  extends ClientCommandBase<Boolean>{
	String firstName;
	String secondName;
	String middleName;
	String login;
	String password;
	String gender;
	String age;
	String sessionId;
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getSecondName() {
		return secondName;
	}
	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}

	public String getMiddleName() {
		return middleName;
	}
	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}

	public String getSessionId() {
		return sessionId;
	}
	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public AddNewUserClientCommand()
	{
		setCommandName("AddNewUserClientCommand");
		setCommandDescription("Registration new user");
		setExecuteMethod(ExecuteMethod.ServerOnly);

	}
	
	@Override
	protected void ExecuteInternal() throws IOException {
		Boolean status = false;
		try {
			ServiceLayer.ServiceFacade sf = new ServiceFacade();
			status = sf.RegistrationUser(getFirstName(), getSecondName(), 
					getMiddleName(), getLogin(), getPassword(), getGender(), 
					Integer.valueOf(getAge()), getSessionId(), getContext().get("HostIP"));
		} catch (Throwable e) {
			ApplicationLogger.getInstance().LOG.error(e.getMessage()); }
		setResult(status);
	}
	
	@Override
	public void FillFromJSON(JSONObject jObj) {
		super.FillFromJSON(jObj);
		setFirstName((String)jObj.get("FirstName"));
		setSecondName((String)jObj.get("SecondName"));
		setMiddleName((String)jObj.get("MiddleName"));
		setLogin((String)jObj.get("Login"));
		setPassword((String)jObj.get("Password"));
		setGender((String)jObj.get("Gender"));
		setAge((String)jObj.get("Age"));
		setSessionId((String)jObj.get("SessionId"));
	}
	@Override
	public String toJSONString() {
			StringBuffer sb = new StringBuffer();
			sb.append("{");
			sb.append("\"" + JSONObject.escape("CommandName") + "\"");                 
			sb.append(":");                 
			sb.append("\"" + JSONObject.escape(getCommandName()) + "\"");                                  
			sb.append(",");                                  
			sb.append("\"" + JSONObject.escape("CommandDescription") + "\"");                 
			sb.append(":");                 
			sb.append("\"" + getCommandDescription() + "\"");   
			sb.append(",");  
			sb.append("\"" + JSONObject.escape("ExecuteMethod") + "\"");    
			sb.append(":");
			sb.append("\"" + getExecuteMethod() + "\"");   
			sb.append(",");
			sb.append("\"" + JSONObject.escape("FirstName") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getFirstName()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("SecondName") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getSecondName()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("MiddleName") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getMiddleName()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("Login") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getLogin()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("Password") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getPassword()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("Gender") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getGender()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("Age") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getAge()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("SessionId") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getSessionId()) + "\"");
		sb.append("}");
		return sb.toString();
	}	

}
